package com.xiaoma.thread.pool;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ThreadPoolTest {
    private static final int COUNT = 10000;
 
    public static void main(String[] args) throws InterruptedException {
    	testThreadPool();
    	testNewThread();
	}
    /**
     * @Description: java线程池实现
     * @param @throws InterruptedException  
     * @author dapengniao
     * @date 2016年4月14日 上午10:16:23
     */
    public static void testThreadPool() throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(COUNT);
        ExecutorService executorService = Executors.newFixedThreadPool(100);
        long bg = System.currentTimeMillis();
        for (int i = 0; i < COUNT; i++) {
	        Runnable command = new TestRunnable(countDownLatch);
	        executorService.execute(command);
        }
        countDownLatch.await();
        System.out.println("testThreadPool:" + (System.currentTimeMillis() - bg));
        executorService.shutdown();
    }
 
    /**
     * @Description: 创建java线程实现
     * @param @throws InterruptedException  
     * @author dapengniao
     * @date 2016年4月14日 上午10:16:31
     */
    public static void testNewThread() throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(COUNT);
        long bg = System.currentTimeMillis();
        for (int i = 0; i < COUNT; i++) {
	        Runnable command = new TestRunnable(countDownLatch);
	        Thread thread = new Thread(command);
	        thread.start();
        }
        countDownLatch.await();
        System.out.println("testNewThread:" + (System.currentTimeMillis() - bg));
    }
 
    private static class TestRunnable implements Runnable {
        private final CountDownLatch countDownLatch;
 
        TestRunnable(CountDownLatch countDownLatch) {
          this.countDownLatch = countDownLatch;
        }
 
        @Override
        public void run() {
           countDownLatch.countDown();
        }
    }
    
    
}
